#include <stdio.h>
#include <stdlib.h>
#include <avr/io.h>
#include <avr/wdt.h>
#include <util/delay.h>
#include <math.h>
#include <avr/pgmspace.h>
#include "backlight.h"
#include "lcd.h"
#include "diag.h"
#include "buttons.h"
#include "utils.h"
#include "pages.h"
#include "em36.h"
#include "body.h"

extern uint16_t w25msOverrun;



/* DIAG page functions */
void DIAG_clear( void ) 
{
	LCD_Box( 0, 44, 132, 150, black );		// blank out body
}

void DIAG_1_init( void )
{
	LCD_Print( "Engine speed",  00, 50, white, black );
	LCD_Print( "Throttle (%)",  00, 60, white, black );
	LCD_Print( "Lambda",  		00, 70, white, black );
	LCD_Print( "Battery (V)",   00, 80, white, black );
	LCD_Print( "Oil (bar)",     00, 90, white, black );
	LCD_Print( "Fuel (bar)",    00, 100, white, black );
	LCD_Print( "Coolant (oC)",	00, 110, white, black );
	LCD_Print( "Body Flags",    00, 130, white, black );
	LCD_Print( "EM36 Flags",    00, 140, white, black );
}

void DIAG_1_update( void )
{
	uint16_t thr;
	thr = EM36_throttle * 100;
	thr = thr >> 8;

	LCD_Print_Int( EM36_engineSpeed, 5, 0, 	102, 50, white, black );
	LCD_Print_Int( thr, 			3, 0, 	114, 60, white, black );
	LCD_Print_Int( EM36_lambda, 	4, 3, 	102, 70, white, black );
	LCD_Print_Int( EM36_battery, 	3, 1, 	108, 80, white, black );
	LCD_Print_Int( EM36_oilP,		3, 1,	108, 90, white, black );
	LCD_Print_Int( EM36_fuelP,      3, 1, 	108, 100, white, black );
	LCD_Print_Int( EM36_coolant,	3, 0,	114, 110, white, black );
	LCD_PrintHex( BODY_flags, 				120, 130, green, black );
	LCD_PrintHex( EM36_flags, 				120, 140, green, black );
}



void DIAG_2_init( void )
{
	LCD_Print( "25ms Overrun",  00, 50, white, black );
	LCD_Print( "CAN EM36 Rx",  	00, 60, white, black );
	LCD_Print( "CAN Body Rx",  	00, 70, white, black );
	LCD_Print( "ADC0",			00, 85, white, black );		LCD_Print( "ADC1",			76, 85, white, black );
	LCD_Print( "ADC2",          00, 95, white, black );     LCD_Print( "ADC3",          76, 95, white, black );
	LCD_Print( "ADC4",          00,105, white, black );
	LCD_Print( "PINA",          00,120, white, black );		LCD_Print( "PINB",          76,120, white, black );
	LCD_Print( "PINC",          00,130, white, black );		LCD_Print( "PIND",          76,130, white, black );
}

void DIAG_2_update( void )
{
	LCD_PrintHex( w25msOverrun, 		120, 50, green, black );
	LCD_PrintHex( EM36_CAN_count, 		120, 60, green, black );
	LCD_PrintHex( BODY_CAN_count, 		120, 70, green, black );
	LCD_PrintHex( BODY_ADC[0], 			54,  85, green, black );	LCD_PrintHex( BODY_ADC[1], 		120,  85, green, black );
	LCD_PrintHex( BODY_ADC[2], 			54,  95, green, black );	LCD_PrintHex( BODY_ADC[3], 		120,  95, green, black );
	LCD_PrintHex( BODY_ADC[4], 			54, 105, green, black );
	LCD_PrintHex( BODY_PortA, 			54, 120, green, black );	LCD_PrintHex( BODY_PortB, 		120, 120, green, black );
	LCD_PrintHex( BODY_PortC, 			54, 130, green, black );	LCD_PrintHex( BODY_PortC, 		120, 130, green, black );
}
